<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['ischecklogin']=$this->model('member')->checklogin('','','','',true);
$shopset=$this->plugin_setting('shopset');
if($_RQ['op']=='main'){
	$return['setting']=$this->plugin_setting('basic');
	$return['list']=array();
	set_html('title','商品评论');
	exi($return);
}
if($_RQ['op']=='commentorder'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}	
	$order_goods=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$id));   
    if($order_goods){
		foreach($order_goods as $k=>&$item){
			$goodinfo=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['goodsid']),array('title','merchid','subtitle','image','price'));          //商品信息
			$goodinfo['image']=tomedia($goodinfo['image']);
			if($goodinfo['merchid']){
				$item['merch']=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goodinfo['merchid']),array('id','merchname'));
			}
			$item=array_merge($goodinfo,$item);
			if($item['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$optionid),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$item['optiontitle']=implode("，",$specs);
			}
			$comment=$this->pdo_get('goods_comments',array('orderid'=>$id,'goodsid'=>$item['goodsid'],'commentid'=>0));		
			if($comment){
				$comment['images']=$comment['images']?json_decode($comment['images'],true):array();
				$item['post']=$comment;
				if($comment['createtime']){
					$comment_changedays=$shopset['comment_changedays']?:0;
					$endpingjiatime = $comment['createtime'] + $comment_changedays*24*3600;
					if($endpingjiatime>TIMESTAMP){
						$item['pingjia']=1;//1修改评价；2追加评价
					}else{
						$zuipingjia = pdo_count('jy_weishop_goods_comments',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$id,'goodsid'=>$item['goodsid'],'commentid >'=>0),'id');
						if($zuipingjia<1){
							$item['pingjia']=2;
						}else{
							$item['pingjia']=3;
						}
					}
				}
				if($comment['show']==1 && $item['pingjia']!=1){
					unset($order_goods[$k]);
				}
			}else{
				$item['post']=array(
					'orderid'=>$id,
					'goodsid'=>$item['goodsid'],
					'star'=>5,
					'sendstar'=>0,
					'fuwustar'=>0,
					'images'=>array(),
				);
			}
			
		}
	}
	$shop_setting=$this->plugin_setting('shop');
	$return['goods_list']=$order_goods;
	$return['shop_setting']=$shop_setting;
	exi($return);
}

if($_RQ['op']=='submit'){
	$postdata=get_postdata();
	$data=array(
		'content'=>$postdata['content'],
		'images'=>ijson_encode($postdata['images']),
		'star'=>intval($postdata['star']),
		'sendstar'=>intval($postdata['sendstar']),
		'fuwustar'=>intval($postdata['fuwustar']),
	);
	$comment_id=$this->pdo_getcolumn('goods_comments',array('orderid'=>intval($postdata['orderid']),'goodsid'=>intval($postdata['goodsid']),'uniacid'=>$_SESSION['uniacid']),'id');
	$merchid=$this->pdo_getcolumn('goods',array('id'=>intval($postdata['goodsid']),'uniacid'=>$_SESSION['uniacid']),'merchid');
	$shopset=$this->plugin_setting('shopset');
	$show=$shopset['iscomment']?0:1;
	if($comment_id){
		if($postdata['show']==1){
			exi('该评价已生效显示，不可修改！','error','goback');
		}
		$this->pdo_update('goods_comments',$data,array('id'=>$comment_id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'show'=>$show,
			'goodsid'=>intval($postdata['goodsid']),
			'orderid'=>intval($postdata['orderid']),
			'createtime'=>TIMESTAMP,
		);
		$data=array_merge($insert,$data);
		$this->pdo_insert('goods_comments',$data);
		$this->update('order',array('iscomment'=>1),array('id'=>$insert['orderid'],'uniacid'=>$_SESSION['uniacid']));
	}
	//评价送券，查找是否有符合条件的满额送券活动
	$price=$this->pdo_getcolumn('order',array('id'=>intval($postdata['orderid']),'uniacid'=>$_SESSION['uniacid']),'price');
	$parameter=array(
		'params'=>array('uniacid'=>$_SESSION['uniacid'],'type'=>1,'status'=>1,'price <='=>$price),
		'orderby'=>'price desc',
		'limit'=>'1',
	);
	$coupons=$this->get_list('coupon_sendtasks',$parameter);
	$coupon=$coupons[0];
	if($coupon['couponid'] && $coupon['num'] >= $coupon['sendnum']){
		$params=array(
			'mid'=>$_SESSION['uid'],
			'ac_type'=>'pingjia',
			'ac_id'=>$coupon['id'],
			'remark'=>'订单评价送券活动',
		);
		if(count($postdata['images']) || !$coupon['is_pingimg']){
			$this->model('coupon')->give_coupon($params);
		}
	}
	if($merchid){
		model('jy_merch/model')->upcomments($postdata,$merchid);
	}
	exi('评价成功！','','goback');
}
?>